﻿using Microsoft.EntityFrameworkCore;
using Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilsORM;
using UtilsORM.MySqlData;

namespace ServiceWeb.DBGateway
{
    public class DBIotDeviceValueLog
    {

        EFYihongIotContext ef = new EFYihongIotContext();

        public List<iot_device_value_log> getHourData(string dguid, string key, int rows)
        {

            string sql = @"SELECT * FROM iot_device_value_log 
WHERE vl_key='" + key + "' and vl_d_guid='" + dguid + "' " +
"and vl_id in( SELECT min(vl_id) FROM iot_device_value_log WHERE vl_key='" + key + "'  and vl_d_guid='" + dguid + "' GROUP BY HOUR(vl_add_time) ) order by vl_id desc limit " + rows + "";
            return MySqlYihongFileServer.GetList<iot_device_value_log>(sql);
        }

        public ModelCharts getModelCharts(string dguid, string key, string name, int rows)
        {

            ModelCharts modelCharts = new ModelCharts();
            Serie serie = new Serie();
            List<Serie> series = new List<Serie>();
            List<string> categories = new List<string>();
            List<string> data = new List<string>();
            List<iot_device_value_log> logs = getHourData(dguid, key, rows).OrderBy(c => c.vl_id).ToList();
            foreach (iot_device_value_log log in logs)
            {
                data.Add(log.vl_value);
                categories.Add(log.vl_add_time.ToString("dd日-HH") + "时");
            }

            serie.name = name;
            serie.data = data;
            series.Add(serie);

            modelCharts.categories = categories;
            modelCharts.series = series;
            return modelCharts;
        }
    }
}
